package com.juzipi.demo.aop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Aspect
@Component
public class WebExceptionAspect {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(* com.juzipi.demo.controller.*.*(..))")
    public void log(){

    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        //接收到请求，记录请求内容
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //记录下请求内容
        String url = request.getRequestURI();
        String method = request.getMethod();
        String ip = request.getRemoteAddr();
        String queryString = request.getQueryString();
        String authType = request.getAuthType();
        Enumeration<String> headerNames = request.getHeaderNames();
        String serverName = request.getServerName();
        int serverPort = request.getServerPort();
        String scheme = request.getScheme();
        StringBuffer requestURL = request.getRequestURL();
        String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        logger.info("<--------------------------------------------------------------------------------------------------------------->");
        logger.info("访问的URL: "+url);
        logger.info("Http方法: "+method);
        logger.info("IP地址: "+ip);
        logger.info("请求触发的方法: "+classMethod);
        logger.info("参数: "+queryString);
        logger.info("身份验证类型: "+authType);
        logger.info("请求头名称: "+headerNames);
        logger.info("请求的URL: "+requestURL);
        logger.info("服务器域名: "+serverName);
        logger.info("服务器端口: "+serverPort);
        logger.info("方式: "+scheme);
        logger.info("<--------------------------------------------------------------------------------------------------------------->");

    }

    @AfterReturning(pointcut = "log()",returning = "retObject")
    public void doAfterReturning(Object retObject){
        //处理完请求，返回内容
//        logger.info("应答值："+retObject.toString());

    }
    //方法抛出异常退出时执行的通知
    @AfterThrowing(pointcut = "log()",throwing = "e")
    //捕获controller类抛出的异常
    public void addAfterThrowingLogger(JoinPoint joinPoint,Exception e){
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        //打印log
        logger.info("<----------------------------------------->");
        logger.info("异常: "+e.getMessage());
        logger.info("异常所在类: "+className);
        logger.info("异常所在的方法: "+methodName);
        logger.info("++++++++++++++++++++++");
        logger.info("异常中的参数: ");
        for (int i = 0;i< args.length; i++){
            logger.info(args[i].toString());
        }
        logger.info("++++++++++++++++++++++");
        logger.info("<----------------------------------------->");
    }


}
